import numpy
#a=[[1,2,3,4],[1,2,3,5],[2,4,5,6],[2,3,4,5],[1,3,4,6],[1,2,3],[1,2,5]]
a=[[1,3,4],[2,3,5],[1,2,3,5],[2,5]]
a=[['r','z','h','j','p'],['z','y','x','w','v','u','t','s'],['z'],
      ['r','x','n','o','s'],['y','r','x','z','q','t','p'],
      ['y','z','x','e','q','s','t','m']]
length=len(a)
#找出所有单项集
c=[]
for i in a:
    for j in i:
        if [j] not in c:
            c.append([j])
c=list(map(frozenset,c))
lc=len(c)
d = {}
for i in c:
    for j in a:
        if i.issubset(j):
            if d.get(i, 0) == 0:
                d[i] = 1
            else:
                d[i] = d[i] + 1
    d[i] = d[i] / length
    if d[i] < 0.5:  # 支持度定义为0.5
        d.pop(i)
items={}
items.update(d)
li = list(d.keys())
l = len(li)
s=2
#找到所有满足支持度的频繁二及以上项集
sup=[]
while l>1:#最后只剩下一项

    li2 = []
    for i in range(l):
        for j in range(i + 1, l):
            if len(li[i] ^ li[j])==2:
                tmp = li[i] | li[j]
                if tmp not in li2:#因为只需要在k个元素的基础上多加一个元素，则两个集合中必定有k-1个元素相同
                    li2.append(tmp)
    c=li2
    d = {}
    for i in c:
        for j in a:
            if i.issubset(j):
                if d.get(i, 0) == 0:
                    d[i] = 1
                else:
                    d[i] = d[i] + 1
        d[i] = d[i] / length
        if d[i] < 0.5:  # 支持度定义为
            # 0.7
            d.pop(i)
    li=list(d.keys())
    l = len(li)
    s=s+1
    if li:
        sup.append(d)
for i in sup:
    items.update(i)








#根据符合支持度的频繁项集产生关联规则     (前件，后件，可信度)
def gennerate_rules(r1,minconp,p):
    l=len(r1)
    li=[]
    for i in range(l):
        for j in range(i+1,l):
            if len(r1[i][1] ^ r1[j][1]) == 2:
                tmp=(r1[i][0]&r1[j][0],r1[i][1]|r1[j][1],p/items.get(r1[i][0]&r1[j][0]))
                if tmp not in li and p/items.get(r1[i][0]&r1[j][0])>=minconp:
                    li.append(tmp)
    return li
# def get_r_all(item):
#     s=list(item.keys())[0]
#     p=item[s]
#     tmp=list(s)
#     r_all=[]
#     r1=[]
#     for i in tmp:
#         r1.append((s-frozenset([i]),frozenset([i])))#先产生后件只有1个的规则
#     r_all.extend(r1)
#     while len(r1[0][0])>=2:
#         r=gennerate_rules(r1)
#         r_all.extend(r)
#         r1=r
#     return r_all,p
#对每一个频繁项集产生关联规则
#从平凡项集到关联规则 前件  后件 置信度
print(items)
def get_all_rules(item,min_conp):
    s = list(item.keys())[0]
    p = item[s]
    tmp = list(s)
    r_all = []
    r1 = []
    for i in tmp:
        if p/items.get(s - frozenset([i]))>=min_conp:
            r1.append((s - frozenset([i]), frozenset([i]),p/items.get(s - frozenset([i]))))  # 先产生后件只有1个的规则
    r_all.extend(r1)
    #再由这些规则合并成新的规则
    while r1 and len(r1[0][0])>=2:
        r=gennerate_rules(r1,min_conp,p)
        r_all.extend(r)
        r1=r
    return r_all
for i ,j in items.items():
    if len(i)>1:#分析二项及以上的项集
        r_all=get_all_rules({i:j},0.5)#为每一份频繁项集产生规则
        for r in r_all:
            print(r)
#一共两个参数，一个支持度，一个置信度